package com.yune.activity.sign;
import android.annotation.SuppressLint;
import android.app.Application;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import com.yune.databinding.ActivitySignBinding;
import com.yune.utils.Network;

import org.json.JSONException;
import org.json.JSONObject;

public class SignViewModel extends AndroidViewModel {
    public static MutableLiveData<String> nickname=new MutableLiveData<>();
    public static MutableLiveData<String> username=new MutableLiveData<>();
    public static MutableLiveData<String> email=new MutableLiveData<>();
    public static MutableLiveData<String> password1=new MutableLiveData<>();
    public static MutableLiveData<String> password2=new MutableLiveData<>();
    @SuppressLint("StaticFieldLeak")
    private SignActivity signActivity;
    public SignViewModel(@NonNull Application application) { super(application); }
    public void setBinding(ActivitySignBinding binding, SignActivity signActivity) {
        nickname.setValue("");
        username.setValue("");
        email.setValue("");
        password1.setValue("");
        password2.setValue("");
        this.signActivity=signActivity;
        
    }
    public void sign(View view){
        if (nickname.getValue().equals("")){
            Toast.makeText(signActivity, "请输入昵称", Toast.LENGTH_SHORT).show();
        }
        if (username.getValue().equals("")){
            Toast.makeText(signActivity, "请输入要注册的账号", Toast.LENGTH_SHORT).show();
        }
        if (email.getValue().equals("")){
            Toast.makeText(signActivity, "请输入邮箱", Toast.LENGTH_SHORT).show();
        }
        if (password1.getValue().equals("")){
            Toast.makeText(signActivity, "请输入密码", Toast.LENGTH_SHORT).show();
        }
        if (password2.getValue().equals("")){
            Toast.makeText(signActivity, "请再次输入密码", Toast.LENGTH_SHORT).show();
        }
        if (!password1.getValue().equals(password2.getValue())){
            Toast.makeText(signActivity, "两次密码不一样", Toast.LENGTH_SHORT).show();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username",username.getValue());
            jsonObject.put("nickname",nickname.getValue());
            jsonObject.put("password",password1.getValue());
            jsonObject.put("email",email.getValue());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Network.post(jsonObject, "https://api2.bmob.cn/1/users", res -> {
            if (res!=null){
                signActivity.runOnUiThread(() -> Toast.makeText(signActivity, "注册成功", Toast.LENGTH_SHORT).show());
                JSONObject jo = new JSONObject();
                try {
                    jo.put("email",email.getValue());
                } catch (JSONException e) { e.printStackTrace(); }
                Network.post(jo, "https://api2.bmob.cn/1/requestEmailVerify", res1 -> {
                    if (res1 !=null){
                        signActivity.runOnUiThread(() -> Toast.makeText(signActivity, "已发送邮箱验证", Toast.LENGTH_SHORT).show());
                    }else {
                        signActivity.runOnUiThread(() -> Toast.makeText(signActivity, "发送邮件失败", Toast.LENGTH_SHORT).show());
                    }
                    signActivity.finish();
                });
            }else{
                signActivity.runOnUiThread(() -> Toast.makeText(signActivity, "注册失败", Toast.LENGTH_SHORT).show());
            }
        });

    }
}
